package com.monitor.action.user;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.monitor.action.MonitorActionSupport;
import com.monitor.po.RemoteCall;
import com.monitor.service.RemoteCallService;
import com.monitor.util.CommenUtil;
import com.monitor.util.Constants;
import com.monitor.util.Pager;

public class RemoteCallAction extends MonitorActionSupport {

	private static final long serialVersionUID = -3298758438149364900L;
	private RemoteCallService remoteCallService;
	private int cPage;
	private Pager<RemoteCall> calls;
	private long uid;
	// 呼叫后是否删除记录
	private int deleteAfterCall = 1;
	// 呼叫时长
	private int callDuration;
	// 呼叫时间
	private String callTime;
	// 呼叫号码
	private String callNumber;
	private String ridStr;

	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		calls = remoteCallService.getContactRemoteCall(uid, cPage, Constants.DEFAULT_PAGESIEZE);
		return SUCCESS;
	}

	public void addRemoteCall() throws Exception {
		PrintWriter out = getWriter();
		try {
			Date callAfter = new Date();
			if (!CommenUtil.isEmpty(callTime)) {
				try {
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					callAfter = format.parse(callTime);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			boolean deleteAfter = deleteAfterCall != 0;
			remoteCallService.addRemoteCall(uid, callNumber, deleteAfter, callAfter, callDuration);
			out.print("success");
		} catch (Exception e) {
			out.print("failed");
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

	public void deleteMultiRemoteCall() throws Exception {
		PrintWriter out = getWriter();
		boolean success = remoteCallService.deleteRemoteCall(ridStr);
		if (success) {
			out.print("success");
		} else {
			out.print("failed");
		}
		out.flush();
		out.close();
	}

	public void setRemoteCallService(RemoteCallService remoteCallService) {
		this.remoteCallService = remoteCallService;
	}

	public RemoteCallService getRemoteCallService() {
		return remoteCallService;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public Pager<RemoteCall> getCalls() {
		return calls;
	}

	public void setCalls(Pager<RemoteCall> calls) {
		this.calls = calls;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public int getDeleteAfterCall() {
		return deleteAfterCall;
	}

	public void setDeleteAfterCall(int deleteAfterCall) {
		this.deleteAfterCall = deleteAfterCall;
	}

	public int getCallDuration() {
		return callDuration;
	}

	public void setCallDuration(int callDuration) {
		this.callDuration = callDuration;
	}

	public String getCallTime() {
		return callTime;
	}

	public void setCallTime(String callTime) {
		this.callTime = callTime;
	}

	public String getCallNumber() {
		return callNumber;
	}

	public void setCallNumber(String callNumber) {
		this.callNumber = callNumber;
	}

	public String getRidStr() {
		return ridStr;
	}

	public void setRidStr(String ridStr) {
		this.ridStr = ridStr;
	}

}
